package edu.mju.scenic.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.mju.scenic.entity.Celebrity;
import edu.mju.scenic.service.CelebrityService;
import edu.mju.serviceutils.ResponseCode;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author CBL
 * @since 2020-12-28
 */
@RestController
@RequestMapping("/scenic/celebrity")
//@CrossOrigin
public class CelebrityController {

    @Autowired
    private CelebrityService celebrityService;

    /**
     * 根据传入参数 新增/修改名人表
     * @param celebrity 传入参数
     * @return 自定义返回
     */
    @PostMapping("/updateCelebrity")
    public ResponseCode updateCelebrity(@RequestBody Celebrity celebrity){
        boolean b = celebrityService.saveOrUpdate(celebrity);
        if(b){
            return ResponseCode.success();
        }
        return ResponseCode.error();
    }

    @DeleteMapping("/deleteCelebrity/{id}")
    public ResponseCode deleteCelebrity(@ApiParam(name = "id" ,value = "删除id") @PathVariable Integer id){
        boolean c = celebrityService.deleteCelebrityById(id);
        return ResponseCode.success();
    }

    @GetMapping("/getCelebrity/{page}/{size}")
    public ResponseCode getCelebrity(@PathVariable Long page,
                                     @PathVariable Long size){
        IPage<Celebrity> celebrityIPage = new Page<>(page,size);
        Map<String,Object> map = celebrityService.getCelebrity(celebrityIPage);
        return ResponseCode.success().data(map).data("page",page);
    }
}

